package com.assessment.service.impl;

import com.assessment.entity.Acomres;
import com.assessment.entity.Acomset;
import com.assessment.entity.Aitemavg;
import com.assessment.entity.Column;
import com.assessment.mapper.AcomsetMapper;
import com.assessment.service.AcomresService;
import com.assessment.service.AcomsetService;
import com.assessment.service.AitemavgService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 鑫十一
 * @since 2021-12-15
 */
@Service
@Transactional
public class AcomsetServiceImpl extends ServiceImpl<AcomsetMapper, Acomset> implements AcomsetService {
    private static final Logger log1 = LoggerFactory.getLogger(AcomsetServiceImpl.class);
    @Autowired(required = false)
    private AitemavgService aitemavgService;
    @Autowired(required = false)
    private AcomresService acomresService;

    @Override
    public List<Column> getColumns() {
        List<Column> columns = new ArrayList<>();
        columns.add(new Column().setLabel("考核").setProp("aname"));
        columns.add(new Column().setLabel("计算名").setProp("name"));
        columns.add(new Column().setLabel("考核项").setProp("itname"));
        columns.add(new Column().setLabel("权重").setProp("weight"));
        return columns;
    }

    @Override
    public boolean startCompute(String id, String name) {
        DecimalFormat df = new DecimalFormat("#.00");

        // 获取全部计算。
        QueryWrapper<Acomset> acomsetQueryWrapper = new QueryWrapper<>();
        acomsetQueryWrapper.eq("aid",id); // 竞赛
        acomsetQueryWrapper.eq("name",name); // 计算名
        List<Acomset> list = this.list(acomsetQueryWrapper); // 计算项
        String aname = list.get(0).getAname();
        Map<String, String> map = new HashMap<>();
        log1.info("计算项及其权重：");
        list.forEach(acomset -> {
            log1.info("循环：{}", acomset);
            map.put(acomset.getItid(),acomset.getWeight());
        });

        QueryWrapper<Aitemavg> aitemavgQueryWrapper = new QueryWrapper<>();
        aitemavgQueryWrapper.eq("aid", id).orderByAsc("pid");
        List<Aitemavg> list1 = aitemavgService.list(aitemavgQueryWrapper); // 所有平均值的列表

        List<Acomres> acomres = new ArrayList<>();
        final String[] pid = {"",""};
        final double[] score = {0.0};
        for (int i = 0; i < list1.size(); i++) {
            Aitemavg aitemavg = list1.get(i);
            log1.info("====当前记录的pid：{}={}",pid[0],pid[1]);
            log1.info("当前分数：{}",score[0]);
            log1.info("平均值项：{}-{}-{}",aitemavg.getItname(), aitemavg.getPname(), aitemavg.getScore());
            if (i == 0) { pid[0] = aitemavg.getPid(); pid[1] = aitemavg.getPname();}
            if (!pid[0].equals(aitemavg.getPid())){ // 不是同一个人
                Acomres acomres1 = new Acomres().setName(name).setAid(id).setAname(aname)
                        .setPid(pid[0]).setPname(pid[1])
                        .setScore(String.valueOf(df.format(score[0]) )).setRanknum("无"); // 添加计算结果
                // 计算结果打印
                pid[0] = aitemavg.getPid(); // 替换当前分数项
                pid[1] = aitemavg.getPname(); // 替换当前分数项
                log1.info("pid不同添加一条：{}", acomres1);
                acomres.add(acomres1);
                score[0]=0;
            }
            log1.info("添加分数==");
            double a = aitemavg.getScore().equals("暂无")?0.0:Double.parseDouble(aitemavg.getScore());
            log1.info("当前的分数：{}", score[0] );
            log1.info("当前的平均分：{}", a);
            log1.info("当前的权重：{}", Double.parseDouble(map.get(aitemavg.getItid())));
            score[0] = score[0] + a * Double.parseDouble(map.get(aitemavg.getItid()) );
            log1.info("计算结果：{}", score[0] );
        }
        if (score[0]!=0){
            acomres.add(new Acomres().setName(name).setAid(id).setAname(aname)
                    .setPid(pid[0]).setPname(pid[1])
                    .setScore(String.valueOf(df.format(score[0]))).setRanknum("无"));
        }
        log1.info("acomres:{}",acomres);
        QueryWrapper<Acomres> acomresQueryWrapper = new QueryWrapper<>();
        acomresQueryWrapper.eq("aid",id);
        acomresQueryWrapper.eq("name",name);
        acomresService.remove(acomresQueryWrapper);

        return acomresService.saveBatch(acomres);
    }
}
